using System.Collections.Generic;
using UnityEngine;

public class SwapPool : MonoBehaviour
{
	public static Dictionary<GameObject, List<GameObject>> pool = new Dictionary<GameObject, List<GameObject>>();

	public static int quota = 400;

	public static void Clear()
	{
		foreach (KeyValuePair<GameObject, List<GameObject>> item in pool)
		{
			pool[item.Key].Clear();
		}
	}

	public static GameObject Add(GameObject gameObject, Vector3 position)
	{
		if (!pool.ContainsKey(gameObject))
		{
			pool.Add(gameObject, new List<GameObject>());
		}
		if (pool[gameObject].Count >= quota)
		{
			return Swap(gameObject, position);
		}
		return Create(gameObject, position);
	}

	public static GameObject Create(GameObject gameObject, Vector3 position)
	{
		GameObject gameObject2 = Object.Instantiate(gameObject, position, Quaternion.identity);
		pool[gameObject].Add(gameObject2);
		return gameObject2;
	}

	public static GameObject Swap(GameObject gameObject, Vector3 position)
	{
		GameObject gameObject2 = pool[gameObject][0].gameObject;
		gameObject2.transform.position = position;
		pool[gameObject].RemoveAt(0);
		pool[gameObject].Add(gameObject2);
		return gameObject2;
	}
}
